import * as THREE from 'three';
import Camera from './camControl';
import CarBody from './carBody';
import {CarProps} from './carProps';
import Grid from './grid'
export default class Model{
    //////////////////////////////////////////// SCENE SETUP 设置场景 /////////////////////////////////////////////
    // 01: Construct the scene 构建场景
    constructor(_main) {
        console.log('场景类')
        // Accelerometer orientation vars
        this.deviceAngle = 0;
        this.pageMain = _main;
        this.vpW = this.pageMain.clientWidth;
        this.vpH = this.pageMain.clientHeight;
        this.props = new CarProps(); // 初始化车辆参数
        // ThreeJS Scene stuff 3js 场景参数
        this.camTarget = new THREE.Vector3(0, 1.0, 1.56);
        this.scene = new THREE.Scene();
        this.renderer = new THREE.WebGLRenderer({ antialias: true });
        this.renderer.setPixelRatio(1);
        this.renderer.setSize(this.vpW, this.vpH);
        this.renderer.setClearColor(0x393533); // 设置场景颜色及其透明度
        this.renderer.autoClear = false; // 渲染每一帧之前不会自动清除其输出
        // Set up cameras // 照相机参数
        this.camOptions = {
            distance: 7,
            focusPos: this.camTarget,
            distRange: { max: 40, min: 4 },
            rotation: new THREE.Vector3(45, 0, 0),
            rotRange: { yMin: -1 },
            eyeSeparation: 0.3
        };
        this.monoc = new Camera(this.camOptions); // 新建照相机
        this.cam = this.monoc;
        this.grid = new Grid(this.scene, this.renderer); // 新建波浪方格
        this.pageMain.appendChild(this.renderer.domElement); // 将3js场景加入到dom对象中
        this.addSceneLights(); // 设置场景灯光
    }
    // 02: Preload has started, move camera up
    introPreloading = function () {
        TweenLite.to(this.cam, 1.0, { distTarget: 30 });
        TweenLite.to(this.cam.rotTarget, 1.0, { x: 0, y: 90 });
    };
    // 03: 预加载结束，新建车辆
    introPreloaded = function (_cargo) {
        // 加载车辆
        this.car = new CarBody(this.scene, _cargo);
        // 加载天空
        this.skyRadiance = _cargo["envSkybox"];
        this.skyRadiance.format = THREE.RGBFormat;
        // 将天空设置为背景
        this.scene.background = this.skyRadiance;
    };
    // 04: Start playing intro animation 引导页切换动画
    introStart = function () {
        this.grid.goToBlackGrid();
        TweenLite.to(this.cam.rotTarget, 1.0, { x: -20, y: 0 });
        TweenLite.to(this.cam, 1.0, { distTarget: 10, distActual: 10 });
        TweenLite.to(this.spotLight, 3.0, { intensity: 1.0 });
        TweenLite.to(this.props, 3.0, { speed: 12.0 });
    };
    // Adds lights to scene
    addSceneLights = function () {
        this.ambLight = new THREE.AmbientLight(0xffffff, 0.2); // 氛围光
        this.spotLight = new THREE.DirectionalLight(0xffffff, 0.0); // 平行光
        this.spotLight.position.set(0, 2, 0); // 设置平行光位置
        this.scene.add(this.spotLight);
        this.scene.add(this.ambLight);
    };
    ////////////////////////////////// CONTROL EVENTS //////////////////////////////////
    onWindowResize = function () {
        this.vpW = this.pageMain.clientWidth;
        this.vpH = this.pageMain.clientHeight;
        this.grid.onWindowResize(this.vpW, this.vpH, this.renderer.getPixelRatio());
        this.car.onWindowResize(this.vpH);
        this.renderer.setSize(this.vpW, this.vpH);
        this.monoc.onWindowResize(this.vpW, this.vpH);
    };
    //////////////////////////////////////////// UPDATE	/////////////////////////////////////////////
    updateIntro = function (time) {
        this.renderer.clear();
        this.grid.update();
        this.cam.update();
        this.renderer.render(this.scene, this.monoc.camera);
    };
    update = function (time, _stereo) {
        if (this.props.update(time) === false) {
            return;
        }
        this.renderer.clear();
        this.car.update(this.props);
        this.grid.update(this.props);
        this.car.carChassis.getWorldPosition(this.camTarget);
        this.cam.setFocusPos(this.camTarget.x, 1.0, this.camTarget.z);
        this.cam.update();
        this.spotLight.position.copy(this.monoc.camera.position).normalize();
        this.spotLight.position.y = 1;
        this.renderer.render(this.scene, this.monoc.camera, null, false);
    };
}